<%@ page import="com.scalar.chiptrack.utils.ConfigManager, java.util.Calendar"%>
<%@ page import="com.scalar.chiptrack.commons.Constants"%>
<%@ page import="com.scalar.chiptrack.commons.SessionKeys"%>
<%@ page import="com.scalar.chiptrack.users.Functionality"%>
<%@ page import="com.scalar.chiptrack.users.User"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>


<%String chipTracContext = "/" + ConfigManager.getChipTracContext(); %>
<% User user1 = (User) session.getAttribute (SessionKeys.USER_KEY); %>

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel=stylesheet>
<SCRIPT language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></SCRIPT>
<SCRIPT language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></SCRIPT>

<SCRIPT language="JavaScript">

var prevPrice = 0;
var binInfoListSize = 0;
var wipFlushDetailsCount = 0;
function storePrevValue( ctrl )
{
        prevPrice = ctrl.value;
}
var dateElement = "";
function openCalendar(dateValue)
{
    dateElement = dateValue;
    newWindow = window.open('<%=chipTracContext%>/jsp/includes/calendar.html','optionsPopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=180,height=440,resizable=no');
    newWindow.moveTo(550,180);
    newWindow.focus();
}
function assignDate(passedValue)
{
    dateElement.value=passedValue;
    window.focus();
}

function loadBinDetails( binNumberCtrl )
{
    var form = document.wipFlushSetupForm;
    form.action = form.action + "?action=<%=Constants.ACTION_LOAD_BIN_DETAILS%>"
    form.submit();
}
function save()
{
    var form = document.wipFlushSetupForm;
    if ( isValidEntry() )
    {
        form.action = form.action + "?action=<%=Constants.ACTION_SAVE%>"
        form.submit();
    }
}
function reset()
{
    var form = document.wipFlushSetupForm;
    form.reset();
}
function isValidEntry()
{
    var form = document.wipFlushSetupForm;
    var pa = document.getElementById( "errors" );
    pa.innerHTML = "";
    for ( i=0; i < wipFlushDetailsCount; i++ )
    {
        var cycleTimeNormalCtrl = form['wipFlushDetailsList['+i+'].cycleTimeNormal'];
        var yieldCtrl = form['wipFlushDetailsList['+i+'].yield'];

        if ( !isValidInteger ( cycleTimeNormalCtrl, true, true ) )
        {
            if ( cycleTimeNormalCtrl.value == '' )
            {
                pa.innerHTML = "<li>Cycle Time Normal should not be blank</li>"
                cycleTimeNormalCtrl.select()
                cycleTimeNormalCtrl.focus()
            }
            else
            {
                pa.innerHTML = "<li>Please enter valid Integer for Cycle Time Normal</li>"
                cycleTimeNormalCtrl.select()
                cycleTimeNormalCtrl.focus()
            }
            return false;
        }
        if ( !isValidYield ( yieldCtrl, true ) || parseFloat ( yieldCtrl.value ) < 1 )
        {
            if ( yieldCtrl.value == '' )
            {
               pa.innerHTML = "<li>Yield should not be blank</li>"
               yieldCtrl.select()
               yieldCtrl.focus()
            }
            else
            {
               pa.innerHTML = "<li>Please enter valid Yield (valid range 1-100)</li>"
               yieldCtrl.select()
               yieldCtrl.focus()
            }
           return false;
        }
    }
    for ( i=0; i < binInfoListSize; i++ )
    {
        var binValueCtrl = form['binInfoList['+ i +'].binValue'];
        if ( binValueCtrl.type == "text" )
        {
            if ( binValueCtrl.value == '' )
            {
                binValueCtrl.value = 0.0;
            }
            else if ( !isValidYield ( binValueCtrl, true ) )
            {
                pa.innerHTML = "<li>Please enter valid Bin Value (valid range 0-100)</li>"
                binValueCtrl.select();
                binValueCtrl.focus();
                return false;
            }
        }
    }
    //TO VALIDATE BIN TOTAL VALUE
    if ( typeof form['selectedBinNumber'] == 'undefined' ) //SORT BINS
    {
        var binTotal = 0;
        for ( j=0; j < binInfoListSize; j++ )
        {
            var binValueCtrl = form['binInfoList['+ j +'].binValue'];
            binTotal = binTotal + parseFloat(binValueCtrl.value);
        }
        if ( parseFloat(binTotal) > 100 )
        {
            pa.innerHTML = "<li>Total Bin value should not exceed 100%</li>"

            return false;
        }
    }
    else
    {
        var binNumbers =  form['selectedBinNumber'].options;
        for ( i = 0; i < binNumbers.length; i++ )
        {
            var binNumber = binNumbers[i].value;
            var binTotal = 0;
            for ( j=0; j < binInfoListSize; j++ )
            {
                var binNumberCtrl = form['binInfoList['+ j +'].binNumber'];
                if ( binNumberCtrl.value == binNumber )
                {
                    var binValueCtrl = form['binInfoList['+ j +'].binValue'];
                    binTotal = binTotal + parseFloat(binValueCtrl.value)
                }
            }
            if ( parseFloat(binTotal) > 100 )
            {
                pa.innerHTML = "<li>" + binNumber + " total value should not exceed 100%</li>"

                return false;
            }
        }
    }
    return true;
}
function isValidInteger ( valueCtrl, isZeroAllowed, isMandatory )
{
    if( valueCtrl.value != '')
    {
        if( !isNumericals(valueCtrl.value) || isNaN(valueCtrl.value) )
        {
             return false;
        }
        if ( !isZeroAllowed )
        {
            if ( parseInt ( valueCtrl.value, 10 ) <= 0 )
            {
                return false;
            }
        }
    }
    else
    {
        if ( isMandatory )
        {
            return false;
        }
    }
    return true;
}
function isValidYield( clCtrl, isMandatory )
{
    if( clCtrl.value != '')
    {
        var j = 0;
        for(var k = 0; k < (clCtrl.value).length; k++)
        {
            var c = (clCtrl.value).charAt(k);
            if( c == '.' )
            {
                j++;
                if(j > 1 )
                {
                    return false;
                }
            }
        }
        if(check_char_position4(clCtrl.value) || (is_Sp_Characters_frieght(clCtrl.value) == false)  )
        {
            return false;
        }
        if( clCtrl.value > 100 )
        {
            return false;
        }
    }
    else
    {
        if ( isMandatory )
        {
            return false;
        }
    }

    return true;
}
function loadProcessStepDetails( selectedProcessStepId, partNumber )
{
    var form = document.wipFlushSetupForm;
    if ( isValidEntry() )
    {
        form.action= form.action + "?action=<%=Constants.ACTION_LOAD_PROCESS_STEP_DETAILS%>&processStepId="+selectedProcessStepId;
        //form.selectedProcessStepId.value = selectedProcessStepId;
        form.partNumber.value = partNumber;

        form.submit();
    }
}
</script>
</HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">

<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->

<TABLE cellSpacing=0 cellPadding=0 width="100%" border="0">
    <TR>
        <td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
            <!-- Side Navigation start -->
            <%@ include file="/jsp/operations/product_menu.jsp" %>
            <!-- end of the side navigations -->
        </td>
        <TD bgcolor="#ffffff"><IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
        <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
        <TD bgcolor="#ffffff"></TD>

        <TD valign="top" width="100%">
            <html:form name="wipFlushSetupForm" action="/wipFlushSetupAction" type="com.scalar.chiptrack.operations.wipflush.forms.WipFlushSetupForm">
                <TABLE border="0" width="100%" align="center">
                    <tr><td width="100%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
                    <tr><td width="100%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
                    <tr>
                        <TD width="100%" align="center" class="NArialW">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Wip Flush Setup Screen</TD>
                    </tr>
                    <tr><td width="100%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
                    <tr>
                        <td width="100%" align="left" class='NArialL'>
                            <b><FONT id="errors" styleClass="NArialL" color="#FF0000"></FONT></b>&nbsp;
                        </td>
                    </tr>
                    <% if ( !user1.isActionAllowed ( Functionality.WIP_FLUSH_SETUP,User.USER_ACTION_MODIFY ) )
                        {
                    %>
                            <tr><td width="100%" align='left'><font class='NArialS'color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b></font></td></tr>
                    <%
                        }
                    %>
                    <tr><td class="NArialL"><chiptrack:errors styleClass="NArialL"  /></td></tr>
                    <tr><td class="NArialL"><chiptrack:info styleClass="NArialL"  /></td></tr>
                    <TR>
                        <nested:hidden property="baseProductName"/>
                        <TD><FONT class='NArialL'>Base Product:</FONT>&nbsp;<FONT class='bodyText'><nested:write property="baseProductName"/></FONT></TD>
                    </TR>
                    <tr>
                        <td width="100%" class="NArialL">
                            <TABLE border="0" width="30%" align="left" class="NArialL">
                                <TR>
                                    <logic:iterate id="wipFlushDetails" name="productFamilyDetailsMap" scope="request">
                                        <TD nowrap bgcolor="#993333" align="center">
                                                <FONT COLOR="#FFFFFF"><bean:write name="wipFlushDetails" property="key"/></FONT>
                                        </TD>
                                        <TD>&nbsp;&nbsp;</TD>
                                    </logic:iterate>
                                </TR>
                                <TR><TD>&nbsp;</TD></TR>
                                <TR>
                                    <% int wipFlushDetailsCount=0; %>
                                    <logic:iterate id="wipFlushDetails" name="productFamilyDetailsMap" scope="request">
                                        <TD nowrap >
                                                <TABLE BORDER="0" class="NArialL">
                                                    <% int leavespaceCount = 0; %>
                                                    <logic:iterate id="product" property="value" name="wipFlushDetails" indexId="idx" type="com.scalar.chiptrack.operations.wipflush.WipFlushDetails">
                                                        <logic:equal property="leaveSpace" name="product" value="true">
                                                            <%leavespaceCount++;%>
                                                            <TR><TD>&nbsp;</td></TR>
                                                        </logic:equal>
                                                    <TR>
                                                        <TD nowrap>
                                                            <TABLE class="smalltext" bgcolor="<%=(leavespaceCount%2 == 0 ) ? "#FFFFEE" : "#DEE1E2"%>">
                                                                <TR>
                                                                    <TD nowrap><B>
                                                                        <logic:equal value="SORT" name="product" property="processTypeName">
                                                                            <A href="javascript:loadProcessStepDetails(<bean:write name="product" property="processStepId"/>,'<bean:write name="product" property="partNumber"/>')"><bean:write name="product" property="partNumber"/></A>
                                                                        </logic:equal>
                                                                        <logic:equal value="Die Bank" name="product" property="processTypeName">
                                                                            <A href="javascript:loadProcessStepDetails(<bean:write name="product" property="processStepId"/>,'<bean:write name="product" property="partNumber"/>')"><bean:write name="product" property="partNumber"/></A>
                                                                        </logic:equal>
                                                                        <logic:notEqual value="SORT" name="product" property="processTypeName">
                                                                            <logic:notEqual value="Die Bank" name="product" property="processTypeName">
                                                                                <bean:write name="product" property="partNumber"/>
                                                                            </logic:notEqual>
                                                                        </logic:notEqual>
                                                                        </B>
                                                                    </TD>
                                                                </TR>
                                                                <%
                                                                    String detailsProp = "/wipFlushDetailsList["+wipFlushDetailsCount+"]";
                                                                %>
                                                                <nested:nest property="<%=detailsProp%>">
                                                                    <nested:hidden property="processStepId"/>
                                                                    <nested:hidden property="locationId"/>
                                                                    <TR><TD nowrap>Cycle Time Normal:</TD><TD><nested:text property="cycleTimeNormal" styleClass="valuetxfield" size="7" maxlength="5" onkeypress="javascript:allowInteger()" onfocus="javascript:this.select();"/></TD></TR>
                                                                    <%
                                                                        if ( "Wafer Store".equalsIgnoreCase( product.getProcessTypeName() ) || "Die Bank".equalsIgnoreCase( product.getProcessTypeName()) || "Class Store".equalsIgnoreCase( product.getProcessTypeName() ) || "Mark & Pack".equalsIgnoreCase( product.getProcessTypeName() ) || "Finished Goods".equalsIgnoreCase( product.getProcessTypeName() ) )
                                                                        {
                                                                    %>
                                                                            <nested:hidden property="yield" value="100"/>
                                                                            <TR><TD>&nbsp;</TD><TD><input type="text" size="7" class="txfield" style="visibility:hidden;"></TD></TR>
                                                                    <%
                                                                        }
                                                                        else
                                                                        {
                                                                    %>
                                                                            <TR><TD nowrap>Yield:</TD><TD><nested:text property="yield" styleClass="valuetxfield" size="7" maxlength="6" onkeyup="javascript:restrictAmt(this,3,2);" onkeypress="javascript:restrictCharacters(this,3,2)" onkeydown="storePrevValue(this)" onfocus="javascript:this.select();"/></TD></TR>
                                                                    <%
                                                                        }
                                                                    %>
                                                                </nested:nest>
                                                                <logic:present name="product" property="packageId">
                                                                    <TR><TD nowrap>Package Id:</TD><TD nowrap><bean:write name="product" property="packageId"/></TD></TR>
                                                                </logic:present>
                                                            </TABLE>
                                                        </TD>
                                                    </TR>
                                                        <%wipFlushDetailsCount++;%>
                                                    </logic:iterate>
                                                </TABLE>
                                        </td>
                                        <TD>&nbsp;&nbsp;</td>
                                    </logic:iterate>
                                    <SCRIPT>
                                        wipFlushDetailsCount = <%=wipFlushDetailsCount%>;
                                    </SCRIPT>
                                </TR>
                            </TABLE>
                        </td>
                    </tr>
                    <TR>
                        <TD>&nbsp;</TD>
                    </TR>
                    <TR>
                        <TD width="100%" class="NArialL">
                           <%int binInfoListSize = 0;%>
                           <nested:hidden property="processTypeName"/>
                            <nested:equal property="processTypeName" value="SORT">
                                <TABLE>
                                    <TR>
                                        <TD class="NArialL">
                                            Part Number:
                                        </TD>
                                        <TD class="bodyText">
                                            <nested:write property="partNumber"/>
                                        </TD>
                                    </TR>

                                    <nested:iterate property="binInfoList" indexId="idx" id="binInfo">
                                        <% binInfoListSize = idx.intValue() +1;  %>
                                        <logic:equal value="0" name="idx">
                                            <TR>
                                                <TD class="NArialL" bgcolor="#993333" align="center"><FONT COLOR="#FFFFFF">Bin Number</FONT></TD>
                                                <TD class="NArialL" bgcolor="#993333" align="center"><FONT COLOR="#FFFFFF">Bin Value</FONT></TD>
                                            </TR>
                                        </logic:equal>
                                        <nested:hidden property="binNumber"/>
                                        <nested:hidden property="processStepId"/>
                                        <TR>
                                            <TD class="NArialL"><nested:write property="binNumber"/>:</TD>
                                            <TD class="NArialL"><nested:text property="binValue" styleClass="valuetxfield" size="5" maxlength="6" onkeyup="javascript:restrictAmt(this,3,2);" onkeypress="javascript:restrictCharacters(this,3,2)" onkeydown="storePrevValue(this)" onfocus="javascript:this.select();"/>%</TD>
                                        </TR>
                                    </nested:iterate>
                                </TABLE>
                            </nested:equal>
                            <nested:equal property="processTypeName" value="Die Bank">
                                <TABLE>
                                    <TR>
                                        <TD class="NArialL">
                                            Part Number:
                                        </TD>
                                        <TD class="bodyText">
                                            <nested:write property="partNumber"/>
                                        </TD>
                                    </TR>
                                    <TR>
                                        <TD class="NArialL">Select Bin:</TD>
                                        <TD class="bodyText">
                                            <nested:select property="selectedBinNumber" styleClass="txfield" onchange="loadBinDetails(this);">
                                                <html:option value="">--Select Bin--</html:option>
                                                <nested:optionsCollection name="wipFlushSetupForm" property="binNumbers" label="label" value="value"/>
                                            </nested:select>
                                        </TD>
                                    </TR>
                                    <nested:notEmpty property="selectedBinNumber">
                                    <TR>
                                        <TD class="NArialL" bgcolor="#993333" align="center"><FONT COLOR="#FFFFFF">Assembly Part Number</FONT></TD>
                                        <TD class="NArialL" bgcolor="#993333" align="center"><FONT COLOR="#FFFFFF">Bin Value</FONT></TD>
                                    </TR>
                                    </nested:notEmpty>
                                    <nested:iterate property="binInfoList" id="binInfo" indexId="idx" type="com.scalar.chiptrack.operations.wipflush.WipFlushBinInfo">
                                        <% binInfoListSize = idx.intValue() +1;  %>

                                        <logic:equal value="<%=binInfo.getBinNumber()%>" name="wipFlushSetupForm" property="selectedBinNumber">
                                            <nested:hidden property="binNumber"/>
                                            <nested:hidden property="processStepId"/>
                                            <nested:hidden property="partNumber"/>
                                            <TR>
                                                <TD class="NArialL"><nested:write property="partNumber"/></TD>
                                                <TD class="NArialL"><nested:text property="binValue" styleClass="valuetxfield" size="5" maxlength="6" onkeyup="javascript:restrictAmt(this,3,2);" onkeypress="javascript:restrictCharacters(this,3,2)" onkeydown="storePrevValue(this)" onfocus="javascript:this.select();"/>%</TD>
                                            </TR>
                                        </logic:equal>
                                        <logic:notEqual value="<%=binInfo.getBinNumber()%>" name="wipFlushSetupForm" property="selectedBinNumber">
                                            <nested:hidden property="binNumber"/>
                                            <nested:hidden property="processStepId"/>
                                            <nested:hidden property="partNumber"/>
                                            <nested:hidden property="binValue" />
                                        </logic:notEqual>
                                    </nested:iterate>
                                </TABLE>
                            </nested:equal>
                        </TD>
                        <SCRIPT>
                            binInfoListSize = <%=binInfoListSize%>;
                        </script>
                    </TR>
                    <TR>
                         <TD align="center">
                                <chiptrack:restrictedLink functionality='<%=Functionality.WIP_FLUSH_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href="javascript:save();" onmouseover="window.status='Click to Save Wip Flush Setup';return true" onmouseout="window.status='';return true"><img src="<%=chipTracContext%>/jsp/includes/images/save.gif" border='0' title="Click to Save Wip Flush Setup"></chiptrack:restrictedLink>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <A href="javascript:reset()"><img src="<%=chipTracContext%>/jsp/includes/images/reset.gif" border='0' title="Click to Reset"></A>
                        </TD>
                    </TR>
                    <tr><td width="100%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20>&nbsp;</td></tr>
                </TABLE>
                <nested:hidden property="baseProductId"/>
                <nested:hidden property="selectedProcessStepId"/>
                <nested:hidden property="partNumber"/>
            </html:form>
        </TD>
        <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
    </TR>
</TABLE>
<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->
</BODY>
</HTML>
